<?php

namespace App\Utils\Helper;

use App\Entity\AdminUser;
use App\Entity\AdminUserGroup;

class GeneralHelper extends BaseHelper {
    /**
     * User: gao
     * Date: 2019/11/29
     * Description: ~
     * @param string $type
     * @param string $message
     */
    public function addFlash (string $type, string $message) {
        if (!$this->container->has('session')) {
            throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
        }

        $this->container->get('session')->getFlashBag()->add($type, $message);
    }

    /**
     * User: Gao
     * Date: 2019-12-27
     * Description: 实现each方法
     * @param $array
     * @return array|bool
     */
    public static function myEach (&$array) {
        $res = [];
        $key = key($array);
        if ($key !== null) {
            next($array);
            $res[1] = $res['value'] = $array[$key];
            $res[0] = $res['key'] = $key;
        } else {
            $res = false;
        }
        return $res;
    }

    /**
     * User: gao
     * Date: 2020/3/20
     * Description: 获取管理员用户组名称
     * @param $id
     * @return string
     */
    public function getAdminUserGroupNameById ($id) {
        $result = '-';

        $obj = $this->entity_manager->getRepository(AdminUserGroup::class)->find($id);
        if (is_object($obj)) {
            $result = $obj->getName();
        }

        return $result;
    }

    /**
     * User: Gao
     * Date: 2020/4/3
     * Description: 是否为超级管理员
     * @param $admin_user_group_id
     * @return bool
     */
    public function isSuperAdmin ($admin_user_group_id) {
        $result = false;

        $admin_user_group = $this->entity_manager->getRepository(AdminUserGroup::class)->findOneBy(['id' => $admin_user_group_id, 'is_super' => true]);
        $result = is_object($admin_user_group);

        return $result;
    }

    /**
     * User: Gao
     * Date: 2020/3/21
     * Description: 获取当前用户可访问功能集合
     * @param $admin_user_group_id
     * @return mixed
     */
    public function getModulesByAdminUserGroupId ($admin_user_group_id) {
        return $this->entity_manager->getRepository(AdminUserGroup::class)->getAuthSignsById($admin_user_group_id);
    }

    /**
     * User: Gao
     * Date: 2020/4/16
     * Description: 随机生成要求位数个字符
     * @param $length
     * @return string|null
     */
    public static function getRandChar ($length) {
        $result = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //字母以及数字
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $result .= $strPol[rand(0, $max)];
        }
        return $result;
    }

    /**
     * 获取用户所处用户组对应的菜单数据
     * @param $admin_user_group_id
     * @return mixed
     */
    public function getMenuData ($admin_user_group_id) {
        return $this->entity_manager->getRepository(AdminUser::class)->getMenuDataByAdminUserGroupId($admin_user_group_id);
    }

    /**
     * 获取用户头像地址
     * @param $avatar
     * @return string
     */
    public static function getAvatar ($avatar = null) {
        if ($avatar) {
            $avatar = GetterHelper::getParameter('website.cdn.domain').$avatar;
        } else {
            $avatar = GetterHelper::getParameter('website.cdn.domain').'uploads/images/logo-300.png';
        }
        return $avatar;
    }

    /**
     * 返回一个相对时间
     * @param \DateTime $date
     * @param string $format
     * @return string
     */
    public function agoFilter (\DateTime $date, $format = 'Y-m-d') {
        $diff = time() - $date->format('U');

        $diff = abs($diff);
        $days = floor($diff / 86400);
        $months = floor($diff / 2592000);

        if ($months >= 12) return $date->format($format);
        if ($months >= 1) return $months.'个月前';

        if ($days >= 1) return $days.'天前';

        if ($diff < 60) return $diff.'秒前';

        $m = floor($diff / 60);
        if ($diff < 120) return '1分钟前';
        if ($diff < 3600) return $m.'分钟前';

        $h = floor($diff / 3600);
        if ($diff < 7200) return '1小时前';
        if ($diff < 86400) return $h.'小时前';

        return $date->format($format);
    }

    /**
     * 字节大小转换
     * @param $size
     * @return string
     */
    public function fileConvertUnit ($size) {
        $result = "";

        if ($size < 1024) {
            $result = round($size, 2).'B';
        } else if ($size < 1048576) {
            $result = round($size / 1024, 2).'K';
        } else if ($size < 1073741824) {
            $result = round($size / 1048576, 2).'M';
        } else {
            $result = round($size / 1073741824, 2).'G';
        }

        return $result;
    }

    /**
     * 获取文件扩展名
     * @param $file_name
     * @return string|string[]
     */
    public function getFileExtension ($file_name) {
        return pathinfo($file_name, PATHINFO_EXTENSION);
    }

    /**
     * 生成新文件名
     * @return string
     */
    public function getUniqueName () {
        return md5(uniqid(microtime(true), true));
    }
}